<template>
  <div>
    <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      width="30%"
      @close="close"
    >
      <el-form ref="form" :model="formModel" label-width="120px" :rules="rules">
        <el-form-item label="权限名称" prop="name">
          <el-input id="name" v-model="formModel.name" style="width: 80%" />
        </el-form-item>
        <el-form-item label="权限标识" prop="code">
          <el-input id="code" v-model="formModel.code" style="width: 80%" />
        </el-form-item>
        <el-form-item label="权限描述" prop="description">
          <el-input id="code" v-model="formModel.description" style="width: 80%" />
        </el-form-item>
        <el-form-item label="开启" prop="enVisible">
          <el-switch
            v-model="formModel.enVisible"
            active-value="1"
            inactive-value="0"
          />
        </el-form-item>
        <el-form-item>
          <el-row type="flex" justify="center">
            <el-col :span="12">
              <el-button size="mini" type="primary" @click="handlePermission">提交</el-button>
              <el-button size="mini" @click="close">取消</el-button>
            </el-col>
          </el-row>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { getPermissionList, addPermission, getPermissionDetail, updatePermission } from '@/api/permission'
export default {
  components: {},
  data() {
    return {
      title: '',
      dialogVisible: false,
      formModel: {
        code: '',
        description: '',
        enVisible: false,
        name: '',
        type: 1,
        pid: 0
      },
      rules: {
        name: [
          { required: true, message: '请输入权限名称', trigger: 'blur' },
          {
            validator: async(rule, val, callback) => {
              let res = await getPermissionList()
              if (this.formModel.id) {
                res = res.filter(item => item.id !== this.formModel.id)
              }
              if (res.some(item => item.name === val)) {
                callback(new Error('权限点名字不能重复'))
              } else {
                callback()
              }
            }, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入权限标识', trigger: 'blur' },
          {
            validator: async(rule, val, callback) => {
              let res = await getPermissionList()
              if (this.formModel.id) {
                res = res.filter(item => item.id !== this.formModel.id)
              }
              if (res.some(item => item.code === val)) {
                callback(new Error('权限标识不能重复'))
              } else {
                callback()
              }
            },
            trigger: 'blur'
          }
        ]
      }
    }
  },
  methods: {
    async open(title, id) {
      this.title = title
      if (title === '编辑权限点') {
        this.formModel = await getPermissionDetail(id)
      } else {
        if (id === 0) {
          this.formModel.type = 1
        } else {
          this.formModel.type = 2
        }
        this.formModel.pid = id
      }
      this.dialogVisible = true
    },
    handlePermission() {
      this.$refs.form.validate(async(valid) => {
        if (valid) {
          if (this.title === '新增权限点') {
            await addPermission(this.formModel)
          } else {
            await updatePermission(this.formModel)
          }
          this.close()
          this.$message.success(`${this.title}成功`)
          this.$emit('getPermissionList')
        }
      })
    },
    close() {
      this.dialogVisible = false
      this.$refs.form.resetFields()
    }
  }
}
</script>

<style scoped>

</style>
